/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PreferenceJFrame.java
 *
 * Created on 2010-03-27, 18:31:01
 */

package com.googlecode.datawander.View;

import com.googlecode.datawander.Settings.PreferencesException;
import com.googlecode.datawander.Presenter.PreferenceControlerImpl;
import com.googlecode.datawander.i18n.I18n;
import com.googlecode.datawander.i18n.I18n.LANGUAGE;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author Marcin Stachniuk
 */
public class PreferenceJFrame extends javax.swing.JFrame {

    private PreferenceControlerInt preferenceControler;
    private static final Logger logger = Logger.getLogger(PreferenceJFrame.class);

    

    /** Creates new form PreferenceJFrame */
    public PreferenceJFrame() throws PreferencesException {
        initComponents();
        preferenceControler = new PreferenceControlerImpl();

        if(preferenceControler.getActualSevedLanguage() == LANGUAGE.ENGLISH) {
            jComboBoxLanguage.setSelectedIndex(1);
        } else {
            jComboBoxLanguage.setSelectedIndex(0);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelChooseLanguage = new javax.swing.JLabel();
        jComboBoxLanguage = new javax.swing.JComboBox();
        jButtonCancel = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();

        setTitle(I18n.getString("preferences"));

        jLabelChooseLanguage.setText(I18n.getString("chooselanguage"));

        jComboBoxLanguage.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Polski", "English" }));

        jButtonCancel.setText(I18n.getString("cancel"));
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jButtonOK.setText(I18n.getString("ok"));
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelChooseLanguage)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(135, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addComponent(jButtonOK)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCancel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelChooseLanguage)
                    .addComponent(jComboBoxLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 135, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancel)
                    .addComponent(jButtonOK))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        try {
            if(jComboBoxLanguage.getSelectedIndex() == 1) {
                if(preferenceControler.getActualSevedLanguage() != LANGUAGE.ENGLISH) {
                    preferenceControler.saveLanguage(LANGUAGE.ENGLISH);
                    showInfoAboutChangesAfterRestart();
                }
            } else {
                if(preferenceControler.getActualSevedLanguage() != LANGUAGE.POLISH) {
                    preferenceControler.saveLanguage(LANGUAGE.POLISH);
                    showInfoAboutChangesAfterRestart();
                }
            }
        } catch(Exception e) {
            logger.debug(I18n.getString("errorinchangelanguage"),e);
            JOptionPane.showMessageDialog(this, 
                    I18n.getString("errorinchangelanguage"),
                    I18n.getString("errortitle"),
                    JOptionPane.ERROR_MESSAGE | JOptionPane.OK_OPTION);
        } finally {
            setVisible(false);
        }
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void showInfoAboutChangesAfterRestart() {
        JOptionPane.showMessageDialog(this, 
                I18n.getString("changeafterrestartapp"),
                I18n.getString("infowindow"), JOptionPane.OK_OPTION);
    }

//    /**
//    * @param args the command line arguments
//    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new PreferenceJFrame().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JComboBox jComboBoxLanguage;
    private javax.swing.JLabel jLabelChooseLanguage;
    // End of variables declaration//GEN-END:variables


}
